<?php


namespace App\Http\Controllers;

use App\Models\Activity;
use App\Models\User;

class ActivityStatisticController extends CommonController
{
    /**
     * 获取活动日发布数据的页面
     */
    public function getDay()
    {
        return view('activitystatistic.index');
    }

    /**
     * 获取活动日发布情况的数据
     */
    public function getDayPub()
    {
        $day = 24 * 3600;
        $today_zero = strtotime('today');
        //获取今天差几个小时
        $rest = sprintf("%.2f", (24 - (time() - $today_zero) / 3600));

        $date = array();
        //获取前31天的每天凌晨的时间戳,过去的时间排在前面
        $stamps = array();
        for ($i = 30; $i >= 0; $i--) {
            $stamps[] = $today_zero - $i * $day;
            $i > 0 && $date[] = date('m-d', strtotime(-$i . " day"));
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            //获取前三十天的活动的发布数据，分类获取
            $activitys = Activity::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->get();
            $result = array('roadshow' => 0, 'onroad' => 0, 'wjdh' => 0, 'xsb' => 0, 'other' => 0,);
            foreach ($activitys as $k => $v) {
                switch ($v->type) {
                    case    'roadshow':
                        $result['roadshow'] = $result['roadshow'] + 1;
                        break;
                    case    'onroad':
                        $result['onroad'] = $result['onroad'] + 1;
                        break;
                    case    'wjdh':
                        $result['wjdh'] = $result['wjdh'] + 1;
                        break;
                    case    'xsb':
                        $result['xsb'] = $result['xsb'] + 1;
                        break;
                    case    'other':
                        $result['other'] = $result['other'] + 1;
                        break;
                }
            }

            $counts[] = $result;
        }

        $today_activitys = Activity::whereBetween('created_at', [$today_zero, time()])->get();
        $today_result = array('roadshow' => 0, 'onroad' => 0, 'wjdh' => 0, 'xsb' => 0, 'other' => 0,);
        foreach ($today_activitys as $k => $v) {
            switch ($v->type) {
                case    'roadshow':
                    $today_result['roadshow'] = $today_result['roadshow'] + 1;
                    break;
                case    'onroad':
                    $today_result['onroad'] = $today_result['onroad'] + 1;
                    break;
                case    'wjdh':
                    $today_result['wjdh'] = $today_result['wjdh'] + 1;
                    break;
                case    'xsb':
                    $today_result['xsb'] = $today_result['xsb'] + 1;
                    break;
                case    'other':
                    $today_result['other'] = $today_result['other'] + 1;
                    break;
            }
        }
        $counts[] = $today_result;
        $date[] = '今天差' . $rest . '小时';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }
//        dd($arr);

        echo json_encode($arr);
    }

    public function getWeekPub()
    {
        //获取本工作周还剩下几天
        $rest = (6 - date('w')) % 7;
        $rest == 0 && $rest = 7;


        //获取上周六凌晨时间戳
        $thisSta = strtotime('today') - (7 - $rest) * 24 * 3600;

        // 定义一周的时间戳
        $week = 7 * 24 * 3600;

        //获取前30周的每周六凌晨的时间戳,从前往后排
        $stamps = array();
        $date = array();

        for ($i = 30; $i >= 0; $i--) {
            $stamps[] = $thisSta - $i * $week;
            $i > 0 && $date[] = '前' . ($i) . '周';
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            //获取前三十周的活动的发布数据，分类获取
            $activitys = Activity::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->get();
            $result = array('roadshow' => 0, 'onroad' => 0, 'wjdh' => 0, 'xsb' => 0, 'other' => 0,);
            foreach ($activitys as $k => $v) {
                switch ($v->type) {
                    case    'roadshow':
                        $result['roadshow'] = $result['roadshow'] + 1;
                        break;
                    case    'onroad':
                        $result['onroad'] = $result['onroad'] + 1;
                        break;
                    case    'wjdh':
                        $result['wjdh'] = $result['wjdh'] + 1;
                        break;
                    case    'xsb':
                        $result['xsb'] = $result['xsb'] + 1;
                        break;
                    case    'other':
                        $result['other'] = $result['other'] + 1;
                        break;
                }
            }

            $counts[] = $result;
        }
        $today_activitys = Activity::whereBetween('created_at', [$thisSta, time()])->get();
        $today_result = array('roadshow' => 0, 'onroad' => 0, 'wjdh' => 0, 'xsb' => 0, 'other' => 0,);
        foreach ($today_activitys as $k => $v) {
            switch ($v->type) {
                case    'roadshow':
                    $today_result['roadshow'] = $today_result['roadshow'] + 1;
                    break;
                case    'onroad':
                    $today_result['onroad'] = $today_result['onroad'] + 1;
                    break;
                case    'wjdh':
                    $today_result['wjdh'] = $today_result['wjdh'] + 1;
                    break;
                case    'xsb':
                    $today_result['xsb'] = $today_result['xsb'] + 1;
                    break;
                case    'other':
                    $today_result['other'] = $today_result['other'] + 1;
                    break;
            }
        }
        $counts[] = $today_result;
        $date[] = '本周差' . $rest . '天';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }

    public function getMonthPub()
    {
        //获取本月还剩下多少天
        $rest = date('t') - date('j') + 1;
        //获取本月1号凌晨的时间戳
        $thisMonth = strtotime(date('Y-m-01'));
        //获取过去十三个月的，每个月1号凌晨的时间戳，从前往后
        $m = date('m');
        $y = date('Y');
        $stamps = array();
        for ($i = 0; $i < 13; $i++) {
            $newM = $m - $i;
            if ($newM <= 0) {
                $newM += 12;
                $y = $y - 1;
            }
            $stamps[] = strtotime($y . '-' . $newM . '-01');
            $y = date('Y');
        }

        $counts = array();
        for ($i = 12; $i > 0; $i--) {
            //获取前12个月的注册数据，过去的数据放在前面
            $activitys = Activity::whereBetween('created_at', [$stamps[$i ], $stamps[$i-1]])->get();
            $result = array('roadshow' => 0, 'onroad' => 0, 'wjdh' => 0, 'xsb' => 0, 'other' => 0,);
            foreach ($activitys as $k => $v) {
                switch ($v->type) {
                    case    'roadshow':
                        $result['roadshow'] = $result['roadshow'] + 1;
                        break;
                    case    'onroad':
                        $result['onroad'] = $result['onroad'] + 1;
                        break;
                    case    'wjdh':
                        $result['wjdh'] = $result['wjdh'] + 1;
                        break;
                    case    'xsb':
                        $result['xsb'] = $result['xsb'] + 1;
                        break;
                    case    'other':
                        $result['other'] = $result['other'] + 1;
                        break;
                }
            }

            $counts[] = $result;
        }

        $date = array();
        for ($i = 12; $i > 0; $i--) {
            //获取前12个月的文字表述，过去的表述放在前面
            $date[] = $m - $i < 1 ? '去年' . ($m - $i + 12) . '月' : $m - $i . '月';
        }

        $today_activitys = Activity::whereBetween('created_at', [$thisMonth, time()])->get();
        $today_result = array('roadshow' => 0, 'onroad' => 0, 'wjdh' => 0, 'xsb' => 0, 'other' => 0,);
        foreach ($today_activitys as $k => $v) {
            switch ($v->type) {
                case    'roadshow':
                    $today_result['roadshow'] = $today_result['roadshow'] + 1;
                    break;
                case    'onroad':
                    $today_result['onroad'] = $today_result['onroad'] + 1;
                    break;
                case    'wjdh':
                    $today_result['wjdh'] = $today_result['wjdh'] + 1;
                    break;
                case    'xsb':
                    $today_result['xsb'] = $today_result['xsb'] + 1;
                    break;
                case    'other':
                    $today_result['other'] = $today_result['other'] + 1;
                    break;
            }
        }
        $counts[] = $today_result;
        $date[] = '本月差' . $rest . '天';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }
}